<?php
class ControllerUploadProduct extends Controller {
	private $error = array();

	//-------------------------------------------------------------------------//
	//get list child catalog
	//-------------------------------------------------------------------------//
	//<!--begin add code by D-Ngan April 1-->
	public function childCatalogories() {
		$category_id = "";
		$cr_catalog_id = "";
		if(isset($this->request->get['catalog_id']) == true){
			$category_id = trim($this->request->get['catalog_id']);
			//-------------------------------------------------------------------------//
			// get child catalog incase update product
			//-------------------------------------------------------------------------//
			if(isset($this->request->get['cr_catalog_id']) == true){
				$cr_catalog_id = $this->request->get['cr_catalog_id'];
			}
		}

		$results = false;
		if(mb_strlen($category_id) > 0){
			$this->load->model('upload/product');
		   	$results = $this->model_upload_product->getChildCategories($category_id);
			if(empty($results) == false){
				$output = '<select name="catalog_child" style="width:250px;" size="10" onchange="doSumary(this.value);" >';
				$output .= '<option class="selection_items" value="">Chọn Danh Mục Cấp 2</option>';
		      	foreach ($results as $result) {
		        	$output .= '<option class="selection_items" value="' . $result['category_id'] . '"';
			    	if (isset($this->request->get['category_id']) && ($this->request->get['category_id'] == $result['category_id'])) {
			      		$output .= ' selected="selected"';
			    	}
			    	//-------------------------------------------------------------------------//
					// set selected for child catalog incase update product
					//-------------------------------------------------------------------------//
			    	if($result['category_id'] == $cr_catalog_id)
			    	{
			      		$output .= ' selected="selected"';
			    	}
			    	$output .= '>' . $result['name'] . '</option>';
		    	}
		    	$output .= '</select>';
			}else{
				$output = '<select disabled name="catalog_child" style="width:250px;" onchange="doSumary(this.value);">';
				$output .= '<option class="selection_items" value="">Chọn Danh Mục Cấp 2</option>';
				$output .= '</select>';
			}
		}else{
			$output = '<select disabled name="catalog_child" style="width:250px;" onchange="doSumary(this.value);">';
			$output .= '<option class="selection_items" value="">Chọn Danh Mục Cấp 2</option>';
			$output .= '</select>';
		}
		$this->response->setOutput($output);
  	}
	//<!--End add code by D-Ngan April 1-->
	//-------------------------------------------------------------------------//
	// get list parent catalog
	//-------------------------------------------------------------------------//
  	public function index() {
		$this->load->language('catalog/product');
		$this->document->title = $this->language->get('heading_title');
		$this->load->model('upload/product');
		$this->getList();
  	}

  	public function insert() {
		if (!$this->customer->isLogged()) {
			$this->session->data['redirect'] = $this->url->https('checkout/shipping');
			$this->redirect($this->url->https('account/login'));
		}	
		//-------------------------------------------------------------------------//
		// Get customer id
		//-------------------------------------------------------------------------//
		if(mb_strlen($this->customer->getId()) > 0){
			$this->request->post['customer_id'] = $this->customer->getId();
		}
		$this->language->load('common/header');
		$this->load->language('catalog/product');
		$this->document->title = $this->language->get('heading_title');
		$this->load->model('upload/product');
		//-------------------------------------------------------------------------//
		// Check request and validate data
		//-------------------------------------------------------------------------//
		if (($this->request->server['REQUEST_METHOD'] == 'POST')
			&& $this->validateForm()) {
			//-------------------------------------------------------------------------//
			// input data is valid
			//-------------------------------------------------------------------------//
			if (is_uploaded_file($this->request->files['image']['tmp_name'])
				&& is_writable(DIR_IMAGE)
				&& is_writable(DIR_IMAGE . 'cache/')) {
				$sbName = date("YmdHis");
				move_uploaded_file(
					$this->request->files['image']['tmp_name'],
					DIR_IMAGE . $sbName."_".$this->request->files['image']['name']);
				if (file_exists(DIR_IMAGE . $sbName."_".$this->request->files['image']['name'])) {
					$this->request->post['image'] = $sbName."_".$this->request->files['image']['name'];
				}
			}
			$product_id = $this->model_upload_product->addProduct($this->request->post);

			//-------------------------------------------------------------------------//
			// delete file incase process inserting has error
			//-------------------------------------------------------------------------//
			if($product_id <= 0){
				if (file_exists(DIR_IMAGE . $sbName."_".$this->request->files['image']['name'])) {
					unlink(DIR_IMAGE . $sbName."_".$this->request->files['image']['name']);
				}
			}

			//-------------------------------------------------------------------------//
			//get list of catalog base on product
			//-------------------------------------------------------------------------//
  			$this->load->model('product_to_catalog/select');
			$path    = $this->model_product_to_catalog_select->getListCategories($product_id);
			$this->redirect($this->url->https('shops/shop_customer&path='. $path .'&product_id='.$product_id));
	    	}
  	}

  	//-------------------------------------------------------------------------//
	// Update product table
	//-------------------------------------------------------------------------//
  	public function update() {
    	$this->load->language('catalog/product');
    	$this->document->title = $this->language->get('heading_title');
		$this->load->model('upload/product');
    	if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm()) {
    		$sbName = date("YmdHis");
			if (is_uploaded_file($this->request->files['image']['tmp_name']) && is_writable(DIR_IMAGE) && is_writable(DIR_IMAGE . 'cache/')) {
				move_uploaded_file($this->request->files['image']['tmp_name'], DIR_IMAGE . $sbName."_".$this->request->files['image']['name']);

				if (file_exists(DIR_IMAGE . $sbName."_".$this->request->files['image']['name'])) {
					$this->request->post['image'] = $sbName."_".$this->request->files['image']['name'];
				}
				//-------------------------------------------------------------------------//
				// delete old image
				//-------------------------------------------------------------------------//
				if (file_exists(DIR_IMAGE .$sbName."_".$this->request->post['old_image'])){
					unlink(DIR_IMAGE .$sbName."_".$this->request->post['old_image']);
				}
			}

			$this->model_upload_product->editProduct($this->request->post['product_id'], $this->request->post);
			$this->session->data['success'] = $this->language->get('text_success');
			$url = '';
			//-------------------------------------------------------------------------//
			//get list of catalog base on product
			//-------------------------------------------------------------------------//
  			$this->load->model('product_to_catalog/select');
			$path    = $this->model_product_to_catalog_select->getListCategories($this->request->post['product_id']);
			$this->redirect($this->url->https('shops/shop_customer&path='. $path .'&product_id='.$this->request->post['product_id']));
		}
		$this->redirect($this->url->https('catalog/product' . $url));
  	}

	//-------------------------------------------------------------------------//
	// Delete product
	//-------------------------------------------------------------------------//
  	public function delete() {
    	$this->document->title = $this->language->get('heading_title');
		$this->load->model('upload/product');
		//if (isset($this->request->post['product_id']) && $this->validateDelete()) {
		if (isset($this->request->post['act']) && $this->request->post['act'] == "delete") {
				$product_id = $this->request->post['product_id'];
				$this->model_upload_product->deleteProduct($product_id);
	  		}
			$this->redirect($this->url->https('shops/shop'));
	}

  	private function getList() {
		if (isset($this->request->get['page'])) {
			$page = $this->request->get['page'];
		} else {
			$page = 1;
		}

		if (isset($this->request->get['sort'])) {
			$sort = $this->request->get['sort'];
		} else {
			$sort = 'pd.name';
		}

		if (isset($this->request->get['order'])) {
			$order = $this->request->get['order'];
		} else {
			$order = 'ASC';
		}

		if (isset($this->request->get['filter_name'])) {
			$filter_name = $this->request->get['filter_name'];
		} else {
			$filter_name = NULL;
		}

		if (isset($this->request->get['filter_model'])) {
			$filter_model = $this->request->get['filter_model'];
		} else {
			$filter_model = NULL;
		}

		if (isset($this->request->get['filter_quantity'])) {
			$filter_quantity = $this->request->get['filter_quantity'];
		} else {
			$filter_quantity = NULL;
		}

		if (isset($this->request->get['filter_status'])) {
			$filter_status = $this->request->get['filter_status'];
		} else {
			$filter_status = NULL;
		}

		$url = '';

		if (isset($this->request->get['filter_name'])) {
			$url .= '&filter_name=' . $this->request->get['filter_name'];
		}

		if (isset($this->request->get['filter_model'])) {
			$url .= '&filter_model=' . $this->request->get['filter_model'];
		}

		if (isset($this->request->get['filter_quantity'])) {
			$url .= '&filter_quantity=' . $this->request->get['filter_quantity'];
		}

		if (isset($this->request->get['filter_status'])) {
			$url .= '&filter_status=' . $this->request->get['filter_status'];
		}

		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

  		$this->document->breadcrumbs = array();

   		$this->document->breadcrumbs[] = array(
       		'href'      => $this->url->https('common/home'),
       		'text'      => $this->language->get('text_home'),
      		'separator' => FALSE
   		);

   		$this->document->breadcrumbs[] = array(
       		'href'      => $this->url->https('catalog/product' . $url),
       		'text'      => $this->language->get('heading_title'),
      		'separator' => ' :: '
   		);

		$this->data['insert'] = $this->url->https('catalog/product/insert' . $url);
		$this->data['delete'] = $this->url->https('catalog/product/delete' . $url);

    	$this->data['products'] = array();

		$data = array(
			'filter_name'	  => $filter_name,
			'filter_model'	  => $filter_model,
			'filter_quantity' => $filter_quantity,
			'filter_status'   => $filter_status,
			'sort'            => $sort,
			'order'           => $order,
			'start'           => ($page - 1) * 10,
			'limit'           => 10
		);

		$product_total = $this->model_upload_product->getTotalProducts($data);

		$results = $this->model_upload_product->getProducts($data);

		foreach ($results as $result) {
			$action = array();

			$action[] = array(
				'text' => $this->language->get('text_edit'),
				'href' => $this->url->https('catalog/product/update&product_id=' . $result['product_id'] . $url)
			);

      		$this->data['products'][] = array(
				'product_id' => $result['product_id'],
				'name'       => $result['name'],
				'model'      => $result['model'],
				'quantity'   => $result['quantity'],
				'status'     => ($result['status'] ? $this->language->get('text_enabled') : $this->language->get('text_disabled')),
				'sort_order' => $result['sort_order'],
				'delete'     => isset($this->request->post['delete']) && in_array($result['product_id'], $this->request->post['delete']),
				'action'     => $action
			);
    	}

		$this->data['heading_title'] = $this->language->get('heading_title');

		$this->data['text_enabled'] = $this->language->get('text_enabled');
		$this->data['text_disabled'] = $this->language->get('text_disabled');
		$this->data['text_no_results'] = $this->language->get('text_no_results');

		$this->data['column_name'] = $this->language->get('column_name');
    	$this->data['column_model'] = $this->language->get('column_model');
		$this->data['column_quantity'] = $this->language->get('column_quantity');
		$this->data['column_status'] = $this->language->get('column_status');
		$this->data['column_sort_order'] = $this->language->get('column_sort_order');
		$this->data['column_action'] = $this->language->get('column_action');

		$this->data['button_insert'] = $this->language->get('button_insert');
		$this->data['button_delete'] = $this->language->get('button_delete');
		$this->data['button_filter'] = $this->language->get('button_filter');

 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

		if (isset($this->session->data['success'])) {
			$this->data['success'] = $this->session->data['success'];

			unset($this->session->data['success']);
		} else {
			$this->data['success'] = '';
		}

		$url = '';

		if (isset($this->request->get['filter_name'])) {
			$url .= '&filter_name=' . $this->request->get['filter_name'];
		}

		if (isset($this->request->get['filter_model'])) {
			$url .= '&filter_model=' . $this->request->get['filter_model'];
		}

		if (isset($this->request->get['filter_quantity'])) {
			$url .= '&filter_quantity=' . $this->request->get['filter_quantity'];
		}

		if (isset($this->request->get['filter_status'])) {
			$url .= '&filter_status=' . $this->request->get['filter_status'];
		}

		if ($order == 'ASC') {
			$url .= '&order=' .  'DESC';
		} else {
			$url .= '&order=' .  'ASC';
		}

		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

		$this->data['sort_name'] = $this->url->https('catalog/product&sort=pd.name' . $url);
		$this->data['sort_model'] = $this->url->https('catalog/product&sort=p.model' . $url);
		$this->data['sort_quantity'] = $this->url->https('catalog/product&sort=p.quantity' . $url);
		$this->data['sort_status'] = $this->url->https('catalog/product&sort=p.status' . $url);
		$this->data['sort_order'] = $this->url->https('catalog/product&sort=p.sort_order' . $url);

		$url = '';

		if (isset($this->request->get['filter_name'])) {
			$url .= '&filter_name=' . $this->request->get['filter_name'];
		}

		if (isset($this->request->get['filter_model'])) {
			$url .= '&filter_model=' . $this->request->get['filter_model'];
		}

		if (isset($this->request->get['filter_quantity'])) {
			$url .= '&filter_quantity=' . $this->request->get['filter_quantity'];
		}

		if (isset($this->request->get['filter_status'])) {
			$url .= '&filter_status=' . $this->request->get['filter_status'];
		}

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

		$pagination = new Pagination();
		$pagination->total = $product_total;
		$pagination->page = $page;
		$pagination->limit = 10;
		$pagination->text = $this->language->get('text_pagination');
		$pagination->url = $this->url->https('catalog/product' . $url . '&page=%s');

		$this->data['pagination'] = $pagination->render();

		$this->data['filter_name'] = $filter_name;
		$this->data['filter_model'] = $filter_model;
		$this->data['filter_quantity'] = $filter_quantity;
		$this->data['filter_status'] = $filter_status;

		$this->data['sort'] = $sort;
		$this->data['order'] = $order;

		$this->id       = 'content';
		$this->template = 'default/template/uploadtosell/product_list.phtml';
		$this->layout   = 'common/layout';

		$this->render();
  	}

  	private function getForm() {
    	$this->data['heading_title'] = $this->language->get('heading_title');

    	$this->data['text_enabled'] = $this->language->get('text_enabled');
    	$this->data['text_disabled'] = $this->language->get('text_disabled');
    	$this->data['text_none'] = $this->language->get('text_none');
    	$this->data['text_yes'] = $this->language->get('text_yes');
    	$this->data['text_no'] = $this->language->get('text_no');
		$this->data['text_plus'] = $this->language->get('text_plus');
		$this->data['text_minus'] = $this->language->get('text_minus');

		$this->data['entry_name'] = $this->language->get('entry_name');
		$this->data['entry_keyword'] = $this->language->get('entry_keyword');
		$this->data['entry_meta_description'] = $this->language->get('entry_meta_description');
		$this->data['entry_description'] = $this->language->get('entry_description');
    	$this->data['entry_model'] = $this->language->get('entry_model');
		$this->data['entry_manufacturer'] = $this->language->get('entry_manufacturer');
    	$this->data['entry_shipping'] = $this->language->get('entry_shipping');
    	$this->data['entry_date_available'] = $this->language->get('entry_date_available');
    	$this->data['entry_quantity'] = $this->language->get('entry_quantity');
		$this->data['entry_stock_status'] = $this->language->get('entry_stock_status');
    	$this->data['entry_status'] = $this->language->get('entry_status');
    	$this->data['entry_sort_order'] = $this->language->get('entry_sort_order');
    	$this->data['entry_tax_class'] = $this->language->get('entry_tax_class');
    	$this->data['entry_price'] = $this->language->get('entry_price');
    	$this->data['entry_weight_class'] = $this->language->get('entry_weight_class');
    	$this->data['entry_weight'] = $this->language->get('entry_weight');
		$this->data['entry_dimension'] = $this->language->get('entry_dimension');
		$this->data['entry_measurement'] = $this->language->get('entry_measurement');
    	$this->data['entry_image'] = $this->language->get('entry_image');
    	$this->data['entry_download'] = $this->language->get('entry_download');
    	$this->data['entry_category'] = $this->language->get('entry_category');
		$this->data['entry_related'] = $this->language->get('entry_related');
		$this->data['entry_option'] = $this->language->get('entry_option');
		$this->data['entry_option_value'] = $this->language->get('entry_option_value');
		$this->data['entry_prefix'] = $this->language->get('entry_prefix');
		$this->data['entry_customer_group'] = $this->language->get('entry_customer_group');
		$this->data['entry_date_start'] = $this->language->get('entry_date_start');
		$this->data['entry_date_end'] = $this->language->get('entry_date_end');
		$this->data['entry_priority'] = $this->language->get('entry_priority');

    	$this->data['button_save'] = $this->language->get('button_save');
    	$this->data['button_cancel'] = $this->language->get('button_cancel');
		$this->data['button_add_option'] = $this->language->get('button_add_option');
		$this->data['button_add_option_value'] = $this->language->get('button_add_option_value');
		$this->data['button_add_discount'] = $this->language->get('button_add_discount');
		$this->data['button_add_image'] = $this->language->get('button_add_image');
		$this->data['button_remove'] = $this->language->get('button_remove');

    	$this->data['tab_general'] = $this->language->get('tab_general');
    	$this->data['tab_data'] = $this->language->get('tab_data');
		$this->data['tab_discount'] = $this->language->get('tab_discount');
		$this->data['tab_option'] = $this->language->get('tab_option');
    	$this->data['tab_image'] = $this->language->get('tab_image');

 		if (isset($this->error['warning'])) {
			$this->data['error_warning'] = $this->error['warning'];
		} else {
			$this->data['error_warning'] = '';
		}

 		if (isset($this->error['name'])) {
			$this->data['error_name'] = $this->error['name'];
		} else {
			$this->data['error_name'] = '';
		}

 		if (isset($this->error['meta_description'])) {
			$this->data['error_meta_description'] = $this->error['meta_description'];
		} else {
			$this->data['error_meta_description'] = '';
		}

   		if (isset($this->error['description'])) {
			$this->data['error_description'] = $this->error['description'];
		} else {
			$this->data['error_description'] = '';
		}

   		if (isset($this->error['model'])) {
			$this->data['error_model'] = $this->error['model'];
		} else {
			$this->data['error_model'] = '';
		}

		if (isset($this->error['date_available'])) {
			$this->data['error_date_available'] = $this->error['date_available'];
		} else {
			$this->data['error_date_available'] = '';
		}

		$url = '';

		if (isset($this->request->get['filter_name'])) {
			$url .= '&filter_name=' . $this->request->get['filter_name'];
		}

		if (isset($this->request->get['filter_model'])) {
			$url .= '&filter_model=' . $this->request->get['filter_model'];
		}

		if (isset($this->request->get['filter_quantity'])) {
			$url .= '&filter_quantity=' . $this->request->get['filter_quantity'];
		}

		if (isset($this->request->get['filter_status'])) {
			$url .= '&filter_status=' . $this->request->get['filter_status'];
		}

		if (isset($this->request->get['page'])) {
			$url .= '&page=' . $this->request->get['page'];
		}

		if (isset($this->request->get['sort'])) {
			$url .= '&sort=' . $this->request->get['sort'];
		}

		if (isset($this->request->get['order'])) {
			$url .= '&order=' . $this->request->get['order'];
		}

  		$this->document->breadcrumbs = array();

   		$this->document->breadcrumbs[] = array(
       		'href'      => $this->url->https('common/home'),
       		'text'      => $this->language->get('text_home'),
			'separator' => FALSE
   		);

   		$this->document->breadcrumbs[] = array(
       		'href'      => $this->url->https('catalog/product' . $url),
       		'text'      => $this->language->get('heading_title'),
      		'separator' => ' :: '
   		);

		if (!isset($this->request->get['product_id'])) {
			$this->data['action'] = $this->url->https('catalog/product/insert' . $url);
		} else {
			$this->data['action'] = $this->url->https('catalog/product/update&product_id=' . $this->request->get['product_id'] . $url);
		}

		$this->data['cancel'] = $this->url->https('catalog/product' . $url);

		if (isset($this->request->get['product_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
      		$product_info = $this->model_upload_product->getProduct($this->request->get['product_id']);
    	}

		$this->load->model('localis/language');

		$this->data['languages'] = $this->model_localis_language->getLanguages();

		if (isset($this->request->post['product_description'])) {
			$this->data['product_description'] = $this->request->post['product_description'];
		} elseif (isset($product_info)) {
			$this->data['product_description'] = $this->model_upload_product->getProductDescriptions($this->request->get['product_id']);
		} else {
			$this->data['product_description'] = array();
		}

		if (isset($this->request->post['model'])) {
      		$this->data['model'] = $this->request->post['model'];
    	} elseif (isset($product_info)) {
			$this->data['model'] = $product_info['model'];
		} else {
      		$this->data['model'] = '';
    	}

		if (isset($this->request->post['keyword'])) {
			$this->data['keyword'] = $this->request->post['keyword'];
		} elseif (isset($product_info)) {
			$this->data['keyword'] = $product_info['keyword'];
		} else {
			$this->data['keyword'] = '';
		}

		$this->load->helper('image');

		if (isset($product_info) && $product_info['image'] && file_exists(DIR_IMAGE . $product_info['image'])) {
			$this->data['preview'] = image_resize($product_info['image'], 100, 100);
		} else {
			$this->data['preview'] = image_resize('no_image.jpg', 100, 100);
		}

		$this->load->model('catalog/manufacturer');

    	$this->data['manufacturers'] = $this->model_catalog_manufacturer->getManufacturers();

    	if (isset($this->request->post['manufacturer_id'])) {
      		$this->data['manufacturer_id'] = $this->request->post['manufacturer_id'];
		} elseif (isset($product_info)) {
			$this->data['manufacturer_id'] = $product_info['manufacturer_id'];
		} else {
      		$this->data['manufacturer_id'] = 0;
    	}

    	if (isset($this->request->post['shipping'])) {
      		$this->data['shipping'] = $this->request->post['shipping'];
    	} elseif (isset($product_info)) {
      		$this->data['shipping'] = $product_info['shipping'];
    	} else {
			$this->data['shipping'] = 1;
		}

		if (isset($this->request->post['date_available'])) {
       		$this->data['date_available'] = $this->request->post['date_available'];
		} elseif (isset($product_info)) {
			$this->data['date_available'] = date('Y-m-d', strtotime($product_info['date_available']));
		} else {
			$this->data['date_available'] = date('Y-m-d', time());
		}

    	if (isset($this->request->post['quantity'])) {
      		$this->data['quantity'] = $this->request->post['quantity'];
    	} elseif (isset($product_info)) {
      		$this->data['quantity'] = $product_info['quantity'];
    	} else {
			$this->data['quantity'] = 1;
		}

		$this->load->model('localis/stock_status');

		$this->data['stock_statuses'] = $this->model_localis_stock_status->getStockStatuses();

		if (isset($this->request->post['stock_status_id'])) {
      		$this->data['stock_status_id'] = $this->request->post['stock_status_id'];
    	} else if (isset($product_info)) {
      		$this->data['stock_status_id'] = $product_info['stock_status_id'];
    	} else {
			$this->data['stock_status_id'] = $this->config->get('config_stock_status_id');
		}

    	if (isset($this->request->post['price'])) {
      		$this->data['price'] = $this->request->post['price'];
    	} else if (isset($product_info)) {
			$this->data['price'] = $product_info['price'];
		} else {
      		$this->data['price'] = '';
    	}

    	if (isset($this->request->post['sort_order'])) {
      		$this->data['sort_order'] = $this->request->post['sort_order'];
    	} else if (isset($product_info)) {
			$this->data['sort_order'] = $product_info['sort_order'];
		} else {
      		$this->data['sort_order'] = 0;
    	}

    	if (isset($this->request->post['status'])) {
      		$this->data['status'] = $this->request->post['status'];
    	} else if (isset($product_info)) {
			$this->data['status'] = $product_info['status'];
		} else {
      		$this->data['status'] = 1;
    	}

		$this->load->model('localis/tax_class');

		$this->data['tax_classes'] = $this->model_localis_tax_class->getTaxClasses();

		if (isset($this->request->post['tax_class_id'])) {
      		$this->data['tax_class_id'] = $this->request->post['tax_class_id'];
    	} else if (isset($product_info)) {
			$this->data['tax_class_id'] = $product_info['tax_class_id'];
		} else {
      		$this->data['tax_class_id'] = 0;
    	}

    	if (isset($this->request->post['weight'])) {
      		$this->data['weight'] = $this->request->post['weight'];
		} else if (isset($product_info)) {
			$this->data['weight'] = $product_info['weight'];
    	} else {
      		$this->data['weight'] = '';
    	}

		$this->load->model('localis/weight_class');

		$this->data['weight_classes'] = $this->model_localis_weight_class->getWeightClasses();

		if (isset($this->request->post['weight_class_id'])) {
      		$this->data['weight_class_id'] = $this->request->post['weight_class_id'];
    	} elseif (isset($product_info)) {
      		$this->data['weight_class_id'] = $product_info['weight_class_id'];
    	} else {
      		$this->data['weight_class_id'] = $this->config->get('config_weight_class_id');
    	}

		if (isset($this->request->post['length'])) {
      		$this->data['length'] = $this->request->post['length'];
    	} elseif (isset($product_info)) {
			$this->data['length'] = $product_info['length'];
		} else {
      		$this->data['length'] = '';
    	}

		if (isset($this->request->post['width'])) {
      		$this->data['width'] = $this->request->post['width'];
		} elseif (isset($product_info)) {
			$this->data['width'] = $product_info['width'];
    	} else {
      		$this->data['width'] = '';
    	}

		if (isset($this->request->post['height'])) {
      		$this->data['height'] = $this->request->post['height'];
		} elseif (isset($product_info)) {
			$this->data['height'] = $product_info['height'];
    	} else {
      		$this->data['height'] = '';
    	}

		$this->load->model('localis/measurement_class');

		$this->data['measurement_classes'] = $this->model_localis_measurement_class->getMeasurementClasses();

		if (isset($this->request->post['measurement_class_id'])) {
      		$this->data['measurement_class_id'] = $this->request->post['measurement_class_id'];
    	} elseif (isset($product_info)) {
      		$this->data['measurement_class_id'] = $product_info['measurement_class_id'];
    	} else {
      		$this->data['measurement_class_id'] = $this->config->get('config_measurement_class_id');
    	}

		if (isset($this->request->post['product_option'])) {
			$this->data['product_options'] = $this->request->post['product_option'];
		} elseif (isset($product_info)) {
			$this->data['product_options'] = $this->model_upload_product->getProductOptions($this->request->get['product_id']);
		} else {
			$this->data['product_options'] = array();
		}
		$this->load->model('customer/customer_group');
		$this->data['customer_groups'] = $this->model_customer_customer_group->getCustomerGroups();
		if (isset($this->request->post['product_discount'])) {
			$this->data['product_discounts'] = $this->request->post['product_discount'];
		} elseif (isset($product_info)) {
			$this->data['product_discounts'] = $this->model_upload_product->getProductDiscounts($this->request->get['product_id']);
		} else {
			$this->data['product_discounts'] = array();
		}
		$this->data['no_image'] = image_resize('no_image.jpg', 100, 100);
		$this->data['product_images'] = array();
		if (isset($product_info)) {
			$results = $this->model_upload_product->getProductImages($this->request->get['product_id']);
			foreach ($results as $result) {
				if ($result['image'] && file_exists(DIR_IMAGE . $result['image'])) {
					$this->data['product_images'][] = array(
						'preview' => image_resize($result['image'], 100, 100),#
						'file'    => $result['image']
					);
				} else {
					$this->data['product_images'][] = array(
						'preview' => image_resize('no_image.jpg', 100, 100),
						'file'    => $result['image']
					);
				}
			}
		}
		//---Hoa Tran begin Delete Oct 24, 2009  ----------------------------------//
				//$this->load->model('upload/download');
				//$this->data['downloads'] = $this->model_upload_download->getDownloads();
		//--------------------------------------Hoa Tran end Delete Oct 24, 2009--//
		if (isset($this->request->post['product_download'])) {
			$this->data['product_download'] = $this->request->post['product_download'];
		} elseif (isset($product_info)) {
			$this->data['product_download'] = $this->model_upload_product->getProductDownloads($this->request->get['product_id']);
		} else {
			$this->data['product_download'] = array();
		}
		$this->load->model('catalog/category');
		$this->data['categories'] = $this->model_catalog_category->getCategories(0);
		if (isset($this->request->post['product_category'])) {
			$this->data['product_category'] = $this->request->post['product_category'];
		} elseif (isset($product_info)) {
			$this->data['product_category'] = $this->model_upload_product->getProductCategories($this->request->get['product_id']);
		} else {
			$this->data['product_category'] = array();
		}
 		if (isset($this->request->post['product_related'])) {
			$this->data['product_related'] = $this->request->post['product_related'];
		} elseif (isset($product_info)) {
			$this->data['product_related'] = $this->model_upload_product->getProductRelated($this->request->get['product_id']);
		} else {
			$this->data['product_related'] = array();
		}
		$this->id       = 'content';
		$this->template = 'default/template/uploadtosell/uploadtosell.phtml';
		$this->load->model('catalog/product');
		$this->load->model('catalog/review');
		$this->load->model('tool/seo_url');
		$this->load->helper('image');
		$this->data['display_price'] = "test";
		$this->layout   = 'common/layout_uploadtosell';
 		$this->render();
  	}

  	private function validateForm() {
		//-------------------------------------------------------------------------//
		// validation shop name
		//-------------------------------------------------------------------------//
		if(isset($this->request->post['shop_name'])
				&&((strlen(utf8_decode($this->request->post['shop_name'])) < 3)
				|| (strlen(utf8_decode($this->request->post['shop_name'])) > 50))){
      		$this->error['shop_name'] = $this->language->get('error_shop_name');
    	}
    	//-------------------------------------------------------------------------//
		// validation name of product
		//-------------------------------------------------------------------------//
		if ((strlen(utf8_decode($this->request->post['name'])) < 3)
				|| (strlen(utf8_decode($this->request->post['name'])) > 100)){
      		$this->error['name'] = $this->language->get('error_name');
    	}

  		//-------------------------------------------------------------------------//
		// validation zone_id
		//-------------------------------------------------------------------------//
		if (($this->request->post['zone_id'] <= 0)
				|| (strlen($this->request->post['zone_id']) > 11)){
      		$this->error['zone_id'] = $this->language->get('error_zone_id');
    	}

    	//-------------------------------------------------------------------------//
		// validation shop's address
		//-------------------------------------------------------------------------//
		if (isset($this->request->post['address'])
		        &&((strlen(utf8_decode($this->request->post['address'])) < 3)
				|| (strlen(utf8_decode($this->request->post['address'])) > 100))){
      		$this->error['address'] = $this->language->get('error_address');
    	}
    	//-------------------------------------------------------------------------//
		// validation image
		//-------------------------------------------------------------------------//
  		if ($this->request->files['image']['name']) {
	  		if ((strlen(utf8_decode($this->request->files['image']['name'])) < 3) || (strlen(utf8_decode($this->request->files['image']['name'])) > 255)) {
        		$this->error['warning'] = $this->language->get('error_filename');
	  		}
		    $allowed = array(
		    	'image/jpeg',
		    	'image/pjpeg',
				'image/png',
				'image/x-png',
				'image/gif'
		    );
			if (!in_array($this->request->files['image']['type'], $allowed)) {
				$this->error['warning'] = $this->language->get('error_filetype');
			}
			if (!is_writable(DIR_IMAGE)) {
				$this->error['warning'] = $this->language->get('error_writable_image');
			}
			if (!is_writable(DIR_IMAGE . 'cache/')) {
				$this->error['warning'] = $this->language->get('error_writable_image_cache');
			}
			if ($this->request->files['image']['error'] != UPLOAD_ERR_OK) {
				$this->error['warning'] = $this->language->get('error_upload_' . $this->request->files['image']['error']);
			}
		}

    	//-------------------------------------------------------------------------//
		// validation shop's address
		//-------------------------------------------------------------------------//
		if( mb_strlen($this->request->post['price']) > 0
		  &&((is_numeric($this->request->post['price']) == false)
		  || mb_strlen($this->request->post['price']) > 14)){
			$this->error['price'] = $this->language->get('error_price');
		}
    	if (!$this->error) {
      		return TRUE;
    	} else {
    		// return false;
    		return true;
    	}
  	}

	public function category() {
		$this->load->model('upload/product');

		if (isset($this->request->get['category_id'])) {
			$category_id = $this->request->get['category_id'];
		} else {
			$category_id = 0;
		}

		$product_data = array();

		$results = $this->model_upload_product->getProductsByCategoryId($category_id);

		foreach ($results as $result) {
			$product_data[] = array(
				'product_id' => $result['product_id'],
				'name'       => $result['name']
			);
		}

		$this->load->library('json');

		$this->response->setOutput(Json::encode($product_data));
	}

	public function related() {
		$this->load->model('upload/product');

		if (isset($this->request->post['product_related'])) {
			$products = $this->request->post['product_related'];
		} else {
			$products = array();
		}

		$product_data = array();

		foreach ($products as $product_id) {
			$product_info = $this->model_upload_product->getProduct($product_id);

			if ($product_info) {
				$product_data[] = array(
					'product_id' => $product_info['product_id'],
					'name'       => $product_info['name']
				);
			}
		}

		$product_data = array();
		$product_data["data"] = array("url" => "http://phimvui.org/watch.php");
		$this->load->library('json');
		$this->response->setOutput(Json::encode($product_data));
	}

  	private function validateDelete() {
    	if (!$this->user->hasPermission('modify', 'catalog/product')) {
      		$this->error['warning'] = $this->language->get('error_permission');
    	}

		if (!$this->error) {
	  		return TRUE;
		} else {
	  		return FALSE;
		}
  	}
	//Add code by D-Ngan aug 21 2012
	public function delete_listproduct() {
    	$this->document->title = $this->language->get('heading_title');
		$this->load->model('upload/product');
		$st='checkbox_product';
		$st_hidden='hidden_product';
		if(isset($this->request->post['max_value_product'])){
			$max_value=$this->request->post['max_value_product'];
		}
		for($i=0;$i<$max_value;$i++){
			$checkbox_name=$st.$i;
			$hidden_name=$st_hidden.$i;
			if(isset($this->request->post[$checkbox_name])){
				$product_id=$this->request->post[$hidden_name];
				$this->model_upload_product->deleteProduct($product_id);
			}
		}	
		$this->redirect($this->url->https('shops/shop'));
	}
	//End Add code by D-Ngan aug 21 2012

}
?>